package com.siyoumi.app.modules.account.service;

import com.siyoumi.app.sys.service.AdminService;
import com.siyoumi.app.sys.vo.ApprVo;
import com.siyoumi.component.http.XHttpContext;
import com.siyoumi.entity.SysAccount;
import com.siyoumi.entity.SysAppRouter;
import com.siyoumi.entity.base.SysAccountBase;
import com.siyoumi.mybatispuls.JoinWrapperPlus;
import com.siyoumi.service.SysAccountService;
import com.siyoumi.service.SysAppRouterService;
import com.siyoumi.service.SysAppService;
import com.siyoumi.component.XSpringContext;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SvcAccount {
    static public SvcAccount getBean() {
        return XSpringContext.getBean(SvcAccount.class);
    }

    static public SysAccountService getApp() {
        return XSpringContext.getBean(SysAccountService.class);
    }

    public SysAccount getEntityByPhone(String phone) {
        JoinWrapperPlus<SysAccount> query = getApp().join();
        query.eq(SysAccountBase.F.acc_x_id, XHttpContext.getX())
                .eq("acc_phone", phone);

        return getApp().first(query);
    }

    /**
     * 获取角色可设置的权限列表
     */
    public List<ApprVo> listSetRoleApprVo() {
        SysAccountService appAcc = SysAccountService.getBean();
        SysAccount entityAcc = appAcc.getSysAccountByToken();

        SysAppRouterService app = SysAppRouterService.getBean();
        List<String> appIds = SysAppService.getBean().getAppIds(entityAcc, true);
        //
        List<String> passIds = notSetAppIds();
        List<String> appIdNew = appIds.stream().filter(appId -> {
            return !passIds.contains(appId);
        }).collect(Collectors.toList());

        List<SysAppRouter> listAppr = app.getList(appIdNew, null);
        return XSpringContext.getBean(AdminService.class).appRouterToApprVo(listAppr);
    }

    /**
     * 不能设置的应用ID
     */
    protected static List<String> notSetAppIds() {
        List<String> apps = new ArrayList<>();
        apps.add("setting");
        apps.add("pwd");
        apps.add("app_admin");
        apps.add("sys_log");
        apps.add("account");
        return apps;
    }
}
